var querySort ="sort_create";
var hash = window.location.hash.slice(1);
var queryGroupId = hash.split(",")[0];
var queryOrgId = hash.split(",")[1];
var pageSize = 8;
var pSize =9;
var windowflag =true;
var queryCount = 0;
var copyTopicId ="";
var shareTopicId ="";
var queryGroupName ="";
var shareGroupId="";//分享组id
var copyTitle="";//复制分享修改的标题
$(function(){
	
	$(window).scroll(function(){
		var viewH =$(this).height();//可见高度
	    var contentH =$(document).height();//内容高度
	    var scrollTop =$(this).scrollTop();//滚动高度
	    if(scrollTop/(contentH -viewH)>=0.95){ //到达底部100px时,加载新内容
	      // 这里加载数据..
    	  if(windowflag&&pSize<queryCount){
    		  windowflag =false;
    		  pSize+=6;
    		  topicList();
		  }
	    }
	})
	$('#copyModal').on('hidden.bs.modal', function () {
		  $("#copyQueryName").val("");
		  queryGroupName="";
		  pageSize=8;
	});
	//将未进入组的成员添加进来,并且修改最后访问时间
	$.ajax({
		url:"../../../hivelearn/group/findGroup.json?id="+queryGroupId+"&groupId="+queryGroupId,
		success:function(){
		}
	})
	$(".uploadBtn").click(function() {
		$("#groupfile").click();
	});
	$('#delModal').modal('hide');
	topicList();
	/**
	 * 组设置
	 */
/*	$(".groupImg .glyphicon").click(function(){
		$(".sortList").slideUp(1000);
		$(".groupMenu").slideDown(1000);
		$(".cardOperating .item").parent().each(function(){
			$(this).slideUp(1000);
		});
	});
	$(".groupMenu .item").click(function(){
		var text = this.innerHTML;
		$(".groupMenu").slideUp(1000);
	});*/
	/**
	 * 筛选
	 */
/*	$(".selSort").click(function(){
		$(".sortList").slideDown(1000);
		$(".groupMenu").slideUp(1000);
		$(".cardOperating .item").parent().each(function(){
			$(this).slideUp(1000);
		});
	});
	$(".sortItem .sortIco").click(function(){
		$(".sortList").slideDown(1000);
		$(".groupMenu").slideUp(1000);
		$(".cardOperating .item").parent().each(function(){
			$(this).slideUp(1000);
		});
	});*/
	/*$(".sortItem").click(function(){
		$(".sortList").slideDown(1000);
		$(".groupMenu").slideUp(1000);
		$(".cardOperating .item").parent().each(function(){
			$(this).slideUp(1000);
		});
	});*/
	$(".sortList .item").click(function(){
		var text = this.innerHTML;
		querySort = $(this).attr("value");
		$(".selSort")[0].innerHTML=text;
		topicList();
		/*$(".sortList").slideUp(1000);*/
	});
	

	/*$(document).bind('click',function(){
		$(".cardOperating .item").parent().each(function(){
			$(this).slideUp(1000);
		});
		$(".groupMenu").slideUp(1000);
		$(".sortList").slideUp(1000);
	});*/
	/*$('.sortItem').bind('click',function(e){ 
		stopPropagation(e); 
	});*/
});
//列表查询
function topicList() {
	$.get('../../../hivelearn/topic/findTopicList.json?querySort=' + querySort+'&queryGroupId='+queryGroupId+'&groupId='+queryGroupId+'&pageSize='+pSize, function(datas) {
		windowflag=true;
		if (datas.success) {
			queryCount=datas.data.query.count;
			for(var i = 0;i<datas.data.topicQuery.resultList.length;i++){
				var d = datas.data.topicQuery.resultList[i];
				/*if(d.topicTitle.length>12){
					d.tName = d.topicTitle.substring(0,12)+"...";
				}else{
					d.tName = d.topicTitle;
				}*/
				if(d.cards.length>0){
					if(d.cards[0]){
						d.firstCardCover = d.cards[0].cardCover;
					}
					if(d.cards[1]){
						d.secondCardCover = d.cards[1].cardCover;
					}
					if(d.cards[2]){
						d.thirdCardCover = d.cards[2].cardCover;
					}
				}
			}
			$(".js-heading-num").text(datas.data.query.count);
			var dom = _.template($('#topicTpl').html());
			dom = dom(datas.data.topicQuery);
			$("#topic").html(dom);
			/*var groupName = datas.data.groupBean.groupName;
			$(".groupName").text(groupName);
			if(groupName.length>15){
				$(".groupName").text(groupName.substring(0,15)+"...");
			}else{
				$(".groupName").text(groupName);
			}
			$(".groupName").attr("title",groupName);
			var ctime = new Date(datas.data.groupBean.createDate);
			var year = ctime.getFullYear();
			var month = ctime.getMonth();
			$(".groupCreateDate").text('（'+year + '年' + month+'月'+'）');
			
			if(datas.data.groupBean.groupCover){
				$(".groupImg").find("img").attr("src","../../../components/gtattachment/open/downloadAttachment.json?attachmentId="
					+datas.data.groupBean.groupCover);
			}*/
			/**
			 * card设置
			 */
			/*$(".cardInfo .glyphicon").click(function(){
				$(".cardOperating .item").parent().each(function(){
					$(this).slideUp(1000);
				});
				$(".groupMenu").slideUp(1000);
				$(".sortList").slideUp(1000);
				$(this).next().slideDown(1000);
			});
			$(".cardOperating .item").click(function(){
				var text = this.innerHTML;
				$(this).parent().slideUp(1000);
			});*/
			/*//防止冒泡
			$('.cardInfo .glyphicon').bind('click',function(e){ 
				stopPropagation(e); 
			});
			$('.groupImg .glyphicon').bind('click',function(e){ 
				stopPropagation(e); 
			});*/
			isShow();
		}
	})
}
function toShareLearning(){
	window.location.href="../../../hivelearnweb/portal/meetingList/meetingList.html#"+queryOrgId;
};
//群组列表查询
function groupList(groupId,noGroupId) {
	$.get('../../../hivelearn/group/findCopyGroupList.json?queryOrgId='+queryOrgId+"&pageSize="+pageSize
			+"&queryGroupName="+queryGroupName+"&queryGroupId="+groupId+"&queryNoGroupId="+noGroupId, function(datas) {
		if (datas.success) {
			var dom;
			if(groupId){
				//复制
				dom = _.template($('#groupTpl').html());
			}else{
				//分享
				dom = _.template($('#shareGroupTpl').html());
			}
			
			var count = datas.data.count;
			dom = dom(datas.data);
			$("#grouplist").html(dom);
			//滚动加载
			$("#copyModal").find(".right-list").scroll(function () {  
                var $this = $(this),  
                viewH = $(this).height(),//可见高度  
                contentH = $(this).get(0).scrollHeight,//内容高度  
                scrollTop = $(this).scrollTop();//滚动高度  
                //if(contentH - viewH - scrollTop <= 100) { //到达底部100px时,加载新内容  
                if (scrollTop / (contentH - viewH) >= 0.98&&count>pageSize) { //到达底部100px时,加载新内容  
                    //console.log("scrollTop："+scrollTop+",contentH："+contentH+",viewH："+viewH);  
                    pageSize+=5;
                    groupList(groupId,noGroupId);
                }  
            }); 
		}
	})
}
/*function stopPropagation(e) { 
	if (e.stopPropagation) 
		e.stopPropagation(); 
	else 
		e.cancelBubble = true; 
}*/
var delTopicId = "";
//删除
function deleteTopic(id){
	delTopicId = id;
	$('#delModal').modal('show');
}
//复制
function copyTopic(id){
	$("#copyModal").find(".right-content-list").show();
	$("#copyModal").find(".right-content-edit").hide();
	copyTopicId = id;
	$("#copyModal").find(".topicImg").empty();
	$("#copyModal").find(".topicTitle").text("");
	$("#copyModal").find(".topicTitle").attr("title","");
	$("#copyModal").find(".title").text("复制");
	$("#copyModal").find(".right-top").text("你想复制到哪里？");
	$.ajax({
		url:'../../../hivelearn/topic/findTopic.json?id=' + id
		+ '&groupId=' + queryGroupId,
		success:function(data){
			if(data.data.topicCover){
				var topicImg = "<img src='../../../components/gtattachment/open/downloadAttachment.json?attachmentId="+data.data.topicCover+"'/>"
				$("#copyModal").find(".topicImg").append(topicImg);
			}
			copyTitle=data.data.topicTitle;
			$("#copyModal").find(".topicTitle").text(data.data.topicTitle);
			$("#copyModal").find(".topicTitle").attr("title",data.data.topicTitle);
			groupList(queryGroupId,"");
			$("#copyQueryName").unbind("keyup");
			$("#copyQueryName").keyup(function(){
				queryGroupName = $(this).val();
				groupList(queryGroupId,"");
			})
			$('#copyModal').modal('show');
		}
	})
}
//分享
function shareTopic(id){
	shareTopicId = id;
	$("#copyModal").find(".right-content-list").show();
	$("#copyModal").find(".right-content-edit").hide();
	$("#copyModal").find(".topicImg").empty();
	$("#copyModal").find(".topicTitle").text("");
	$("#copyModal").find(".topicTitle").attr("title","");
	$("#copyModal").find(".title").text("分享");
	$("#copyModal").find(".right-top").text("你想分享到哪里？");
	$.ajax({
		url:'../../../hivelearn/topic/findTopic.json?id=' + id
		+ '&groupId=' + queryGroupId,
		success:function(data){
			if(data.data.topicCover){
				var topicImg = "<img src='../../../components/gtattachment/open/downloadAttachment.json?attachmentId="+data.data.topicCover+"'/>"
				$("#copyModal").find(".topicImg").append(topicImg);
			}
			copyTitle=data.data.topicTitle;
			$("#copyModal").find(".topicTitle").text(data.data.topicTitle);
			$("#copyModal").find(".topicTitle").attr("title",data.data.topicTitle);
			groupList("",queryGroupId);
			$("#copyQueryName").unbind("keyup");
			$("#copyQueryName").keyup(function(){
				queryGroupName = $(this).val();
				groupList("",queryGroupId);
			})
			$('#copyModal').modal('show');
		}
	})
}
function delTopic(){
	$.ajax({
		url:"../../../hivelearn/topic/deleteTopicById.json?id="+delTopicId+"&groupId="+queryGroupId,
		success:function(data){
			$('#delModal').modal('hide');
			delTopicId="";
			topicList();
		},
		error:function(data){
			$('#delModal').modal('hide');
			delTopicId="";
			topicList();
		}
	})
}
//修改
function updateTopic(id){
	window.location.href="./meetingThemeEdit.html#"+id+","+queryGroupId+","+queryOrgId+","+1;
}
function addTopic(){
	window.location.href="./meetingThemeAdd.html#"+queryGroupId+","+queryOrgId;
}
function saveCopyTopic(id){
	$.ajax({
		url:"../../../hivelearn/topic/copyTopic.json",
		data:{"groupId":id,"topicId":copyTopicId,"topicTitle":copyTitle},
		success:function(data){
			$('#copyModal').modal('hide');
			copyTopicId="";
			topicList();
		}
	});
}
//显示分享修改页面
/*function shareTopicInfo(groupId,groupName){
	shareGroupId = groupId;
	var title = $("#copyModal").find(".topicTitle").text();
	$("#copyModal").find(".right-content-list").hide();
	$("#copyModal").find(".right-content-edit").show();
	$("#updateTopicTitle").val(title);
	$("#copyModal").find(".right-top").text("分享到："+groupName);
	$("#copyModal").find(".right-top").attr("title","分享到："+groupName);
	//返回
	$("#copyModal").find(".share-btn").find(".arrow").click(function(){
		$("#copyModal").find(".right-content-list").show();
		$("#copyModal").find(".right-content-edit").hide();
		$("#updateTopicTitle").val("");
		$("#copyModal").find(".right-top").text("你想分享到哪里？");
		$("#copyModal").find(".right-top").attr("title","");
		$("#copyModal").find(".error-msg").text("");
	});
	$("#copyModal").find("#updateTopicTitle").keyup(function(){
		if($(this).val()){
			$("#copyModal").find(".error-msg").text("");
		}
	});
}*/
//保存分享
function saveShareTopic(shareGroupId){
	$.ajax({
		url:"../../../hivelearn/topic/shareTopic.json",
		data:{"groupId":shareGroupId,"topicId":shareTopicId,"topicTitle":copyTitle},
		success:function(data){
			$('#copyModal').modal('hide');
			shareTopicId="";
			topicList();
		}
	});
}
//跳转主题详情页
function toTopicDetails(id){
	window.location.href="../studyTalk/studyTalk.html#"+id+","+queryGroupId+","+queryOrgId;
}
//人员列表
function peoplelist(){
	window.location.href="../peopleList/peopleList.html#"+queryGroupId+","+queryOrgId;
}
//活动列表
function activitylist(){
	window.location.href="../activityInfo/activityInfo.html#"+queryGroupId+","+queryOrgId;
}

//修改复制，分享标题
function toUpdateTitle(e){
	$(e).hide();
	var $title = $(e).siblings(".topicTitle");
	var title= $title.text();
	$title.empty();
	$title.append("<input type='text' class='updateCopyTitle' maxlength='40' value='"+title+"'/>");
	$title.find(".updateCopyTitle").focus();
	$title.find(".updateCopyTitle").blur(function(){
		var $val = $(this).val();
		if($val){
			copyTitle=$val;
		}
		$title.empty();
		$title.text(copyTitle);
		$(e).show();
	})
}